package com.blockmeta.bbs.baselibrary.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * share以xml存放，不同进程可共享，但不同步，需要重新加载sp才能同步
 */
public class SharedPreferenceUtil {

	public static SharedPreferences sp;
	private static String NAME = null;

	public SharedPreferenceUtil(Context context) {
		NAME = context.getApplicationContext().getClass().getSimpleName();
	}

	// 存boolean值
	public static void putBoolean(Context context,  String key, boolean value) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		sp.edit().putBoolean(key, value).commit();
	}

	// 取boolean值
	public static boolean getBoolean(Context context, String key,
									 boolean defValue) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		return sp.getBoolean(key, defValue);
	}

	public static void putString(Context context, String key, String value) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		sp.edit().putString(key, value).commit();
	}

	public static String getString(Context context, String key,
								   String defValue) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		return sp.getString(key, defValue);
	}

	public static void putInt(Context context, String key, int value) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		sp.edit().putInt(key, value).commit();
	}

	public static int getInt(Context context, String key,
							 int defValue) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		return sp.getInt(key, defValue);
	}

	public static void putLong(Context context, String key, long value) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		sp.edit().putLong(key, value).commit();
	}

	public static Long getLong(Context context, String key,
							   long defValue) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		return sp.getLong(key, defValue);
	}

	public static void putFloat(Context context, String key, Float value) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		sp.edit().putFloat(key, value).commit();
	}

	public static Float getFloat(Context context, String key,
								 float defValue) {
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		return sp.getFloat(key, defValue);
	}
	public static void remove(Context context, String key)
	{
		if (sp == null) {
			sp = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
		}
		SharedPreferences.Editor ed = sp.edit();
		if (null != ed)
		{
			ed.remove(key);
			ed.commit();
		}
	}
}
